﻿Imports PoliceHealthClass

Public Class frmCancerResultHistory
    Private arrEditRow As New ArrayList
    Delegate Sub delLoadPoliceCancerResultHistories(ByVal year As String, ByVal organizationId As String)

    Private Sub frmCancerResultHistory_Load(sender As System.Object, e As System.EventArgs) Handles MyBase.Load
        DBUtilities.BindOrganizatoin(trvOrganization)
        DBUtilities.BindingYear(cbbYear)
    End Sub

    Private Sub cbbYear_SelectionChangeCommitted(sender As System.Object, e As System.EventArgs) Handles cbbYear.SelectionChangeCommitted
        If trvOrganization.SelectedNode.IsSelected Then
            LoadDataGridView()
        End If
    End Sub

    Private Sub LoadDataGridView()
        ConnectionString.ShowLoadingForm()
        Me.Invoke(New delLoadPoliceCancerResultHistories(AddressOf LoadPoliceCancerResultHistories), cbbYear.SelectedValue.ToString(), trvOrganization.SelectedNode.Tag.ToString())
        ConnectionString.TerminateLoadingForm()
        'LoadPoliceCancerResultHistories(cbbYear.SelectedValue.ToString(), trvOrganization.SelectedNode.Tag.ToString())
    End Sub

    Private Sub LoadPoliceCancerResultHistories(ByVal year As String, ByVal organizationId As String)
        Dim policeCancerResultHistories As IList(Of CancerResultHistory) = CancerResultHistory.GetPoliceCancerByYearAndOrganizationId(year, organizationId)
        Dim intRowCount As Integer = 0
        Dim gvmBuffer As PoliceHealthSystem.DataGridViewMaskedTextBox.DataGridViewMaskedTextBoxColumn
        Dim mtbBuffer As New MaskedTextBox
        arrEditRow = New ArrayList
        dgvCancerResult.Rows.Clear()

        For Each policeCancerResultHistory As CancerResultHistory In policeCancerResultHistories
            dgvCancerResult.Rows.Add()
            dgvCancerResult.Rows(intRowCount).Cells("colOrder").Value = intRowCount + 1
            dgvCancerResult.Rows(intRowCount).Cells("colCitizenId").Value = policeCancerResultHistory.CitizenID
            dgvCancerResult.Rows(intRowCount).Cells("colFullName").Value = policeCancerResultHistory.FullName
            dgvCancerResult.Rows(intRowCount).Cells("colAge").Value = policeCancerResultHistory.Age

            gvmBuffer = dgvCancerResult.Columns("colAFP")
            mtbBuffer.Mask = gvmBuffer.Mask
            mtbBuffer.Text = IIf(policeCancerResultHistory.Afp Is Nothing, String.Empty, String.Format("{0:" + mtbBuffer.Mask + "}", policeCancerResultHistory.Afp))
            dgvCancerResult.Rows(intRowCount).Cells("colAFP").Value = mtbBuffer.Text

            gvmBuffer = dgvCancerResult.Columns("colPSA")
            mtbBuffer.Mask = gvmBuffer.Mask
            mtbBuffer.Text = IIf(policeCancerResultHistory.Psa Is Nothing, String.Empty, String.Format("{0:" + mtbBuffer.Mask + "}", policeCancerResultHistory.Psa))
            dgvCancerResult.Rows(intRowCount).Cells("colPSA").Value = mtbBuffer.Text

            gvmBuffer = dgvCancerResult.Columns("colCEA")
            mtbBuffer.Mask = gvmBuffer.Mask
            mtbBuffer.Text = IIf(policeCancerResultHistory.Cea Is Nothing, String.Empty, String.Format("{0:" + mtbBuffer.Mask + "}", policeCancerResultHistory.Cea))
            dgvCancerResult.Rows(intRowCount).Cells("colCEA").Value = mtbBuffer.Text

            gvmBuffer = dgvCancerResult.Columns("colCA125")
            mtbBuffer.Mask = gvmBuffer.Mask
            mtbBuffer.Text = IIf(policeCancerResultHistory.Ca_125 Is Nothing, String.Empty, String.Format("{0:" + mtbBuffer.Mask + "}", policeCancerResultHistory.Ca_125))
            dgvCancerResult.Rows(intRowCount).Cells("colCA125").Value = mtbBuffer.Text

            intRowCount += 1
        Next

        lblPoliceQuantity.Text = dgvCancerResult.Rows.Count.ToString()

        If dgvCancerResult.Rows.Count > 0 Then
            btnSave.Enabled = True
        Else
            btnSave.Enabled = False
        End If
    End Sub

    Private Sub trvOrganization_AfterSelect(sender As System.Object, e As System.Windows.Forms.TreeViewEventArgs) Handles trvOrganization.AfterSelect
        lblSelectedOrganization.Text = trvOrganization.SelectedNode.Text
        LoadDataGridView()
    End Sub

    Private Sub dgvCancerResult_CellMouseClick(sender As System.Object, e As System.Windows.Forms.DataGridViewCellMouseEventArgs) Handles dgvCancerResult.CellMouseClick
        If dgvCancerResult IsNot Nothing Then

            Dim point1 As Point = dgvCancerResult.CurrentCellAddress
            If point1.X = e.ColumnIndex And _
                point1.Y = e.RowIndex And _
                e.Button = Windows.Forms.MouseButtons.Left And _
                Not dgvCancerResult.EditMode = _
                DataGridViewEditMode.EditProgrammatically Then

                dgvCancerResult.BeginEdit(True)

            End If
        End If

    End Sub

    Private Sub dgvCancerResult_CellEnter(sender As System.Object, e As System.Windows.Forms.DataGridViewCellEventArgs) Handles dgvCancerResult.CellEnter
        dgvCancerResult.BeginEdit(True)
        If Not arrEditRow.Contains(e.RowIndex) Then
            arrEditRow.Add(e.RowIndex)
        End If
    End Sub

    Private Sub btnSave_Click(sender As System.Object, e As System.EventArgs) Handles btnSave.Click
        Dim blResults As Boolean = True
        Dim errorMessage As String = String.Empty
        Dim returnFromParse As Decimal = 0

        If Management.verifyInsert() Then
            For Each RowIndex As Integer In arrEditRow
                Dim police As New CancerResultHistory()
                police.Year = cbbYear.SelectedValue.ToString()
                police.CitizenID = dgvCancerResult.Rows(RowIndex).Cells("colCitizenId").Value.ToString()
                police.OrganizationID = trvOrganization.SelectedNode.Tag.ToString()
                police.OrganzationName = lblSelectedOrganization.Text
                police.FullName = dgvCancerResult.Rows(RowIndex).Cells("colFullName").Value.ToString()
                police.Age = dgvCancerResult.Rows(RowIndex).Cells("colAge").Value.ToString()

                If Not Decimal.TryParse(dgvCancerResult.Rows(RowIndex).Cells("colAFP").Value.ToString(), returnFromParse) Then
                    Exit For
                End If

                If Not Decimal.TryParse(dgvCancerResult.Rows(RowIndex).Cells("colPSA").Value.ToString(), returnFromParse) Then
                    Exit For
                End If

                If Not Decimal.TryParse(dgvCancerResult.Rows(RowIndex).Cells("colCEA").Value.ToString(), returnFromParse) Then
                    Exit For
                End If

                If Not Decimal.TryParse(dgvCancerResult.Rows(RowIndex).Cells("colCA125").Value.ToString(), returnFromParse) Then
                    Exit For
                End If

                police.Afp = dgvCancerResult.Rows(RowIndex).Cells("colAFP").Value.ToString().Trim(" ", ".").Replace(" ", "0")
                police.Psa = dgvCancerResult.Rows(RowIndex).Cells("colPSA").Value.ToString().Trim(" ", ".").Replace(" ", "0")
                police.Cea = dgvCancerResult.Rows(RowIndex).Cells("colCEA").Value.ToString().Trim(" ", ".").Replace(" ", "0")
                police.Ca_125 = dgvCancerResult.Rows(RowIndex).Cells("colCA125").Value.ToString().Trim(" ", ".").Replace(" ", "0")
                police.Afp_abnormal_status = CancerResultHistory.CheckAFPAbnormal(police.Afp)
                police.Psa_abnormal_status = CancerResultHistory.CheckPSAAbnormal(police.Psa)
                police.Cea_abnormal_status = CancerResultHistory.CheckCEAAbnormal(police.Cea)
                police.Ca_125_abnormal_status = CancerResultHistory.CheckCA125Abnormal(police.Ca_125)
                police.CreatedUser = ConnectionString._username
                police.UpdatedUser = ConnectionString._username
                police.CreateDate = Date.Now
                police.UpdateDate = Date.Now

                If Not CancerResultHistory.InsertPoliceCancerResultHistory(police, errorMessage) Then
                    MessageBox.Show("ไม่สามารถบันทึกข้อมูลของ " & police.FullName & " ได้เนื่องจาก : " & errorMessage, "ข้อผิดพลาด", MessageBoxButtons.OK, MessageBoxIcon.Error)
                    blResults = False
                    Exit For
                End If

            Next

            If blResults Then
                ConnectionString.ShowLoadingForm()
                Me.Invoke(New delLoadPoliceCancerResultHistories(AddressOf LoadPoliceCancerResultHistories), cbbYear.SelectedValue.ToString(), trvOrganization.SelectedNode.Tag.ToString())
                ConnectionString.TerminateLoadingForm()
                Management.showResultInsert(blResults)
            End If
        End If

    End Sub
End Class